/**
 * 
 */
package com.qiyou.dao.impl;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.qiyou.dao.IPhotoAlbumDao;
import com.qiyou.entity.PhotoAlbum;
import com.qiyou.util.DBUtil;
import com.qiyou.util.TransformUtil;

/**
 * @author 赛艳志
 *
 */
public class PhotoAlbumDaoImpl implements IPhotoAlbumDao {
    private Connection conn;
    
    public PhotoAlbumDaoImpl(Connection conn){
        this.conn = conn;
    }
    
    @Override
    public boolean doCreate(PhotoAlbum album) throws SQLException {
        String sql = "insert into tb_photo_album(userID,paName,paDate) values(?,?,?)";
        Object[] objects = TransformUtil.beanTOArray(album, true);
        return DBUtil.execute(conn, sql, objects);
    }

    @Override
    public boolean doUpdate(PhotoAlbum album) throws SQLException {
        String sql = "update tb_photo_album set paID=?,userID=?,paName=?,paDate=? where paID=?";
        Object[] objects = TransformUtil.beanTOArray(album, false);
        return DBUtil.execute(conn, sql, objects,objects[0]);
    }

    @Override
    public boolean doDelete(long albumID) throws SQLException {
        String sql = "delete from tb_photo_album where paID=?";
        return DBUtil.execute(conn, sql, albumID);
    }

    @Override
    public PhotoAlbum getPhotoAlbum(long albumID) throws SQLException {
        PhotoAlbum photoAlbum = null;
        String sql = "select * from tb_photo_album where paID=?";
        List<Object[]> all = DBUtil.query(conn, sql, albumID);
        if (null!=all&&all.size()>0) {
            photoAlbum = (PhotoAlbum) TransformUtil.arrayToBean(all.get(0),PhotoAlbum.class);
        }
        return photoAlbum;
    }

    @Override
    public List<PhotoAlbum> getPhotoAlbums(String userID) throws SQLException {
        List<PhotoAlbum> palist = null;
        String sql = "select * from tb_photo_album where userID=?";
        List<Object[]> all = DBUtil.query(conn, sql, userID);
        if (null!=all&&all.size()>0) {
            palist = new ArrayList<PhotoAlbum>(all.size());
            PhotoAlbum album = null;
            for (Object[] objects : all) {
                album = (PhotoAlbum) TransformUtil.arrayToBean(objects, PhotoAlbum.class);
                palist.add(album);
            }
        }
        return palist;
    }

    @Override
    public List<PhotoAlbum> getPhotoAlbums(Object... params)
            throws SQLException {
        List<PhotoAlbum> palist = null;
        StringBuffer sql = new StringBuffer("select * from tb_photo_album ");
        if (null != params && params.length > 0) {
            for (Object param : params) {
                sql.append(param);
            }
        }
        List<Object[]> all = DBUtil.query(conn, sql.toString());
        if (null!=all&&all.size()>0) {
            palist = new ArrayList<PhotoAlbum>(all.size());
            PhotoAlbum album = null;
            for (Object[] objects : all) {
                album = (PhotoAlbum) TransformUtil.arrayToBean(objects, PhotoAlbum.class);
                palist.add(album);
            }
        }
        return palist;
    }

}
